<template>
  <div v-if="backtestResults.equityCurve && backtestResults.equityCurve.length" class="backtest-results">
    <el-divider></el-divider>
    <h3>回测结果摘要</h3>
    
    <el-row :gutter="20">
      <el-col :span="8">
        <el-card shadow="hover">
          <div class="result-item">
            <div class="result-label">总收益率</div>
            <div class="result-value" :class="{ 'up': backtestSummary.totalReturn > 0, 'down': backtestSummary.totalReturn < 0 }">
              {{ backtestSummary.totalReturn > 0 ? '+' : '' }}{{ backtestSummary.totalReturn.toFixed(2) }}%
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="8">
        <el-card shadow="hover">
          <div class="result-item">
            <div class="result-label">年化收益率</div>
            <div class="result-value" :class="{ 'up': backtestSummary.annualReturn > 0, 'down': backtestSummary.annualReturn < 0 }">
              {{ backtestSummary.annualReturn > 0 ? '+' : '' }}{{ backtestSummary.annualReturn.toFixed(2) }}%
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="8">
        <el-card shadow="hover">
          <div class="result-item">
            <div class="result-label">夏普比率</div>
            <div class="result-value">{{ backtestSummary.sharpeRatio.toFixed(2) }}</div>
          </div>
        </el-card>
      </el-col>
    </el-row>
    
    <el-row :gutter="20" style="margin-top: 20px;">
      <el-col :span="8">
        <el-card shadow="hover">
          <div class="result-item">
            <div class="result-label">最大回撤</div>
            <div class="result-value">{{ backtestSummary.maxDrawdown.toFixed(2) }}%</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="8">
        <el-card shadow="hover">
          <div class="result-item">
            <div class="result-label">胜率</div>
            <div class="result-value">{{ backtestSummary.winRate.toFixed(2) }}%</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="8">
        <el-card shadow="hover">
          <div class="result-item">
            <div class="result-label">总交易次数</div>
            <div class="result-value">{{ backtestSummary.totalTrades }}</div>
          </div>
        </el-card>
      </el-col>
    </el-row>
    
    <!-- 回测图表 -->
    <div class="backtest-chart-container" style="margin-top: 20px; height: 400px;">
      <div ref="backtestChart" style="width: 100%; height: 100%;"></div>
    </div>
    
    <!-- 交易记录 -->
    <el-divider></el-divider>
    <h3>交易记录</h3>
    <!-- 回测结果统计 -->
    <el-card shadow="hover" style="margin-top: 20px;">
      <template #header>
        <div class="card-header">
          <span>回测结果统计</span>
        </div>
      </template>
      <div class="result-stats">
        <el-row :gutter="20">
          <el-col :span="8">
            <div class="stat-item">
              <div class="stat-label">总收益率</div>
              <div class="stat-value" :class="backtestSummary.totalReturn >= 0 ? 'profit' : 'loss'">
                {{ backtestSummary.totalReturn.toFixed(2) }}%
              </div>
            </div>
          </el-col>
          <el-col :span="8">
            <div class="stat-item">
              <div class="stat-label">年化收益率</div>
              <div class="stat-value" :class="backtestSummary.annualReturn >= 0 ? 'profit' : 'loss'">
                {{ backtestSummary.annualReturn.toFixed(2) }}%
              </div>
            </div>
          </el-col>
          <el-col :span="8">
            <div class="stat-item">
              <div class="stat-label">夏普比率</div>
              <div class="stat-value">
                {{ backtestSummary.sharpeRatio.toFixed(2) }}
              </div>
            </div>
          </el-col>
        </el-row>
        <el-row :gutter="20" style="margin-top: 20px;">
          <el-col :span="8">
            <div class="stat-item">
              <div class="stat-label">最大回撤</div>
              <div class="stat-value loss">
                {{ backtestSummary.maxDrawdown.toFixed(2) }}%
              </div>
            </div>
          </el-col>
          <el-col :span="8">
            <div class="stat-item">
              <div class="stat-label">胜率</div>
              <div class="stat-value">
                {{ backtestSummary.winRate.toFixed(2) }}%
              </div>
            </div>
          </el-col>
          <el-col :span="8">
            <div class="stat-item">
              <div class="stat-label">总交易次数</div>
              <div class="stat-value">
                {{ backtestSummary.totalTrades }}
              </div>
            </div>
          </el-col>
        </el-row>
      </div>
    </el-card>

    <!-- 交易记录表格 -->
    <el-card shadow="hover" style="margin-top: 20px;">
      <template #header>
        <div class="card-header">
          <span>交易记录</span>
        </div>
      </template>
      <el-table :data="backtestTrades" style="width: 100%;" stripe>
        <el-table-column prop="date" label="日期" width="120" sortable />
        <el-table-column prop="type" label="类型" width="80">
          <template #default="scope">
            <el-tag :type="scope.row.type === 'buy' ? 'success' : 'danger'" size="small">
              {{ scope.row.type === 'buy' ? '买入' : '卖出' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="price" label="价格" width="120">
          <template #default="scope">
            ${{ scope.row.price.toFixed(2) }}
          </template>
        </el-table-column>
        <el-table-column prop="amount" label="数量" width="120" />
        <el-table-column prop="value" label="价值" width="120">
          <template #default="scope">
            ${{ scope.row.value.toFixed(2) }}
          </template>
        </el-table-column>
        <el-table-column prop="profit" label="盈亏" width="120" v-if="backtestTrades.some(t => t.profit !== undefined)">
          <template #default="scope">
            <span v-if="scope.row.profit !== undefined" 
                  :class="scope.row.profit >= 0 ? 'profit' : 'loss'">
              ${{ scope.row.profit.toFixed(2) }}
            </span>
            <span v-else>-</span>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'BacktestResults',
  props: {
    backtestResults: {
      type: Object,
      required: true
    },
    backtestSummary: {
      type: Object,
      required: true
    },
    backtestTrades: {
      type: Array,
      required: true
    }
  },
  mounted() {
    // 在这里可以初始化图表
    this.$nextTick(() => {
      if (this.backtestChart) {
        // 初始化图表的代码可以放在这里
      }
    })
  }
}
</script>

<style scoped>
.result-item {
  text-align: center;
  padding: 20px 0;
}

.result-label {
  font-size: 14px;
  color: #909399;
  margin-bottom: 10px;
}

.result-value {
  font-size: 24px;
  font-weight: bold;
}

.result-value.up {
  color: #14b143;
}

.result-value.down {
  color: #ef232a;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.result-stats {
  padding: 20px 0;
}

.stat-item {
  text-align: center;
  padding: 15px;
}

.stat-label {
  font-size: 14px;
  color: #909399;
  margin-bottom: 10px;
}

.stat-value {
  font-size: 20px;
  font-weight: bold;
}

.stat-value.profit {
  color: #14b143;
}

.stat-value.loss {
  color: #ef232a;
}

.stat-value.neutral {
  color: #f59e0b;
}
</style>